<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>API | minigame-canvas-engine</title>
    <meta name="description" content="轻量级canvas渲染引擎">
    <meta name="generator" content="VuePress 1.3.1">
    <meta name="robots" content="all">
  <meta name="author" content="yuanzm">
  <meta name="keywords" content="minigame-canvans-engine, minigame, canvas, engine, game engine">
    
    <link rel="preload" href="/minigame-canvas-engine/assets/css/0.styles.cc9e188e.css" as="style"><link rel="preload" href="/minigame-canvas-engine/assets/js/app.34da408a.js" as="script"><link rel="preload" href="/minigame-canvas-engine/assets/js/2.b58d2eb8.js" as="script"><link rel="preload" href="/minigame-canvas-engine/assets/js/6.8bafe2e6.js" as="script"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/10.525d02f5.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/3.65046f77.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/4.e50764d8.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/5.aa96439f.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/7.6ddd2960.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/8.5202cd3c.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/9.b0c2371a.js">
    <link rel="stylesheet" href="/minigame-canvas-engine/assets/css/0.styles.cc9e188e.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/minigame-canvas-engine/" class="home-link router-link-active"><!----> <span class="site-name">minigame-canvas-engine</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"> <a href="https://github.com/wechat-miniprogram/minigame-canvas-engine" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"> <a href="https://github.com/wechat-miniprogram/minigame-canvas-engine" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><a href="/minigame-canvas-engine/" class="sidebar-link">概览</a></li><li><section class="sidebar-group depth-0"><a href="/minigame-canvas-engine/api/guide" class="sidebar-heading clickable open"><span>文档</span> <!----></a> <ul class="sidebar-links sidebar-group-items"><li><a href="/minigame-canvas-engine/api/guide.html" class="sidebar-link">快速入门</a></li><li><a href="/minigame-canvas-engine/api/api.html" class="active sidebar-link">API</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/api.html#layout" class="sidebar-link">Layout</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/api.html#getelementsbyid" class="sidebar-link">getElementsById</a></li><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/api.html#getelementsbyclassname" class="sidebar-link">getElementsByClassName</a></li><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/api.html#updateviewport" class="sidebar-link">updateViewPort</a></li><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/api.html#clear" class="sidebar-link">clear</a></li><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/api.html#clearpool" class="sidebar-link">clearPool</a></li><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/api.html#clearall" class="sidebar-link">clearAll</a></li><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/api.html#loadimgs" class="sidebar-link">loadImgs</a></li><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/api.html#registbitmapfont" class="sidebar-link">registBitMapFont</a></li></ul></li><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/api.html#事件api" class="sidebar-link">事件API</a></li><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/api.html#属性api" class="sidebar-link">属性API</a></li></ul></li><li><a href="/minigame-canvas-engine/api/tags.html" class="sidebar-link">标签</a></li><li><a href="/minigame-canvas-engine/api/style.html" class="sidebar-link">样式</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="api"><a href="#api" class="header-anchor">#</a> API</h1> <h2 id="layout"><a href="#layout" class="header-anchor">#</a> Layout</h2> <h3 id="getelementsbyid"><a href="#getelementsbyid" class="header-anchor">#</a> getElementsById</h3> <h4 id="layout-getelementsbyid-string-elementid"><a href="#layout-getelementsbyid-string-elementid" class="header-anchor">#</a> Layout.getElementsById(String elementId)</h4> <p>获取元素id为<strong>elementId</strong>的一组元素</p> <h3 id="getelementsbyclassname"><a href="#getelementsbyclassname" class="header-anchor">#</a> getElementsByClassName</h3> <h4 id="layout-getelementsbyclassname-string-classname"><a href="#layout-getelementsbyclassname-string-classname" class="header-anchor">#</a> Layout.getElementsByClassName(String className)</h4> <p>获取包含class为<strong>className</strong>的一组元素</p> <h3 id="updateviewport"><a href="#updateviewport" class="header-anchor">#</a> updateViewPort</h3> <h4 id="layout-updateviewport-object-box"><a href="#layout-updateviewport-object-box" class="header-anchor">#</a> Layout.updateViewPort(Object box)</h4> <p>更新被绘制canvas的窗口信息，本渲染引擎并不关心是否会和其他游戏引擎共同使用，而本身又需要支持事件处理，因此，如果被渲染内容是绘制到离屏canvas，需要将最终绘制在屏幕上
的绝对尺寸和位置信息更新到本渲染引擎。</p> <h5 id="box"><a href="#box" class="header-anchor">#</a> box</h5> <table><thead><tr><th>key</th> <th>类型</th> <th>是否必填</th> <th>说明</th></tr></thead> <tbody><tr><td>width</td> <td>Number</td> <td>是</td> <td>canvas的物理像素宽度</td></tr> <tr><td>heigth</td> <td>Number</td> <td>是</td> <td>canvas的物理像素高度</td></tr> <tr><td>x</td> <td>Number</td> <td>是</td> <td>canvas 距离屏幕左上角的物理像素x坐标</td></tr> <tr><td>y</td> <td>Number</td> <td>是</td> <td>canvas 距离屏幕左上角的物理像素y坐标</td></tr></tbody></table> <h4 id="layout-getelementsbyclassname-string-classname-2"><a href="#layout-getelementsbyclassname-string-classname-2" class="header-anchor">#</a> Layout.getElementsByClassName(String className)</h4> <p>获取包含class为<strong>className</strong>的一组元素</p> <h3 id="clear"><a href="#clear" class="header-anchor">#</a> clear</h3> <h4 id="layout-clear"><a href="#layout-clear" class="header-anchor">#</a> Layout.clear()</h4> <p>清理画布，之前的计算出来的渲染树也会一并清理，此时可以再次执行init和layout方法渲染界面。</p> <h3 id="clearpool"><a href="#clearpool" class="header-anchor">#</a> clearPool</h3> <h4 id="layout-clearpool"><a href="#layout-clearpool" class="header-anchor">#</a> Layout.clearPool()</h4> <p>调用此API可以清理对象池，释放内存</p> <h3 id="clearall"><a href="#clearall" class="header-anchor">#</a> clearAll</h3> <h4 id="layout-clearall"><a href="#layout-clearall" class="header-anchor">#</a> Layout.clearAll()</h4> <p>等价于按序调用Layout.clear和Layout.clearPool.</p> <h3 id="loadimgs"><a href="#loadimgs" class="header-anchor">#</a> loadImgs</h3> <h4 id="layout-loadimgs-array-imgarr"><a href="#layout-loadimgs-array-imgarr" class="header-anchor">#</a> Layout.loadImgs(Array imgarr)</h4> <p>对于图片资源，如果不提前加载，渲染过程中可能出现挨个出现图片效果，影响体验。通过Layout.loadImgs可以预加载图片资源，在调用Layout.layout的时候渲染性能更好，体验更佳。</p> <div class="language- extra-class"><pre class="language-text"><code>// 注意图片路径不需要加./作为前缀，以小游戏根目录作为根目录
Layout.loadImgs([
    'sub/Buffet_icon_GiftPlate_0.png',
    'sub/Buffet_icon_GiftPlate.png',
    'sub/UI_Icon_Rating.png',
]);

</code></pre></div><h3 id="registbitmapfont"><a href="#registbitmapfont" class="header-anchor">#</a> registBitMapFont</h3> <h4 id="layout-registbitmapfont-name-src-config"><a href="#layout-registbitmapfont-name-src-config" class="header-anchor">#</a> Layout.registBitMapFont(name, src, config)</h4> <table><thead><tr><th>keyName</th> <th>类型</th> <th>描述</th></tr></thead> <tbody><tr><td>name</td> <td>String</td> <td>字体的名称</td></tr> <tr><td>src</td> <td>String</td> <td>bitMapFont字体的图片链接</td></tr> <tr><td>config</td> <td>String</td> <td>BitMapFont的配置信息</td></tr></tbody></table> <div class="language-js extra-class"><pre class="language-js"><code>Layout<span class="token punctuation">.</span><span class="token function">registBitMapFont</span><span class="token punctuation">(</span>
<span class="token string">'fnt_number-export'</span><span class="token punctuation">,</span>
<span class="token string">'https://res.wx.qq.com/wechatgame/product/webpack/userupload/20200314/fnt_number-export.png'</span><span class="token punctuation">,</span>
<span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">info face=&quot;fnt_number-export&quot; size=50 bold=0 italic=0 charset=&quot;&quot; unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1
common lineHeight=60 base=26 scaleW=190 scaleH=181 pages=1 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0
page id=0 file=&quot;fnt_number-export.png&quot;
chars count=15
char id=31561 x=0 y=61 width=60 height=61 xoffset=0 yoffset=2 xadvance=57 page=0 chnl=0 letter=&quot;等&quot;
char id=32423 x=0 y=0 width=62 height=60 xoffset=0 yoffset=2 xadvance=59 page=0 chnl=0 letter=&quot;级&quot;
char id=46 x=168 y=116 width=21 height=21 xoffset=0 yoffset=39 xadvance=18 page=0 chnl=0 letter=&quot;.&quot;
char id=49 x=145 y=0 width=27 height=57 xoffset=0 yoffset=3 xadvance=24 page=0 chnl=0 letter=&quot;1&quot;
char id=50 x=44 y=123 width=41 height=57 xoffset=0 yoffset=3 xadvance=38 page=0 chnl=0 letter=&quot;2&quot;
char id=51 x=102 y=58 width=40 height=57 xoffset=0 yoffset=3 xadvance=37 page=0 chnl=0 letter=&quot;3&quot;
char id=52 x=143 y=58 width=40 height=57 xoffset=0 yoffset=3 xadvance=37 page=0 chnl=0 letter=&quot;4&quot;
char id=53 x=0 y=123 width=43 height=57 xoffset=0 yoffset=3 xadvance=40 page=0 chnl=0 letter=&quot;5&quot;
char id=54 x=127 y=116 width=40 height=57 xoffset=0 yoffset=3 xadvance=37 page=0 chnl=0 letter=&quot;6&quot;
char id=55 x=86 y=119 width=40 height=57 xoffset=0 yoffset=3 xadvance=37 page=0 chnl=0 letter=&quot;7&quot;
char id=56 x=63 y=0 width=40 height=57 xoffset=0 yoffset=3 xadvance=37 page=0 chnl=0 letter=&quot;8&quot;
char id=57 x=104 y=0 width=40 height=57 xoffset=0 yoffset=3 xadvance=37 page=0 chnl=0 letter=&quot;9&quot;
char id=48 x=61 y=61 width=40 height=57 xoffset=0 yoffset=3 xadvance=37 page=0 chnl=0 letter=&quot;0&quot;
char id=32 x=0 y=0 width=0 height=0 xoffset=0 yoffset=0 xadvance=0 page=0 chnl=0 letter=&quot; &quot;
char id=9 x=0 y=0 width=0 height=0 xoffset=0 yoffset=0 xadvance=0 page=0 chnl=0 letter=&quot;	&quot;

kernings count=0</span><span class="token template-punctuation string">`</span></span>
<span class="token punctuation">)</span>

</code></pre></div><h2 id="事件api"><a href="#事件api" class="header-anchor">#</a> 事件API</h2> <p>通过getElementsById或者getElementsByClassName获取元素之后，可以的绑定事件，支持的事件有<code>touchstart</code>、<code>touchmove</code>、<code>touchend</code>、<code>touchcancel</code>、<code>click</code>,示例如下：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> list <span class="token operator">=</span> Layout<span class="token punctuation">.</span><span class="token function">getElementsByClassName</span><span class="token punctuation">(</span><span class="token string">'listItem'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

list<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">item</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    item<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'touchstart'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token punctuation">,</span> item<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="属性api"><a href="#属性api" class="header-anchor">#</a> 属性API</h2> <p>通过Layout.getElementsById和Layout.getElementsByClassName获取image和text之后，修改图片的链接或者文本的值会自动重渲染界面。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> img <span class="token operator">=</span> Layout<span class="token punctuation">.</span><span class="token function">getElementsById</span><span class="token punctuation">(</span><span class="token string">'testimgid'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
img<span class="token punctuation">.</span>src <span class="token operator">=</span> <span class="token string">'newimgsrc'</span><span class="token punctuation">;</span>

<span class="token keyword">let</span> text <span class="token operator">=</span> Layout<span class="token punctuation">.</span><span class="token function">getElementsById</span><span class="token punctuation">(</span><span class="token string">'tesettextid'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
text<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token string">'newtextvalue'</span><span class="token punctuation">;</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/wechat-miniprogram/minigame-canvas-engine/edit/master/api/api.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">3/15/2020, 11:28:23 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/minigame-canvas-engine/api/guide.html" class="prev">
        快速入门
      </a></span> <span class="next"><a href="/minigame-canvas-engine/api/tags.html">
        标签
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/minigame-canvas-engine/assets/js/app.34da408a.js" defer></script><script src="/minigame-canvas-engine/assets/js/2.b58d2eb8.js" defer></script><script src="/minigame-canvas-engine/assets/js/6.8bafe2e6.js" defer></script>
  </body>
</html>
